<template>
  <div class="cv-wrap">
    <template v-if="resumeIdLoadding">
      <div id="cv-detail">
        <div class="left">
          <div class="search-container">
            <div class="title">应聘详情</div>
            <div class="wrap">
              <div class="search-input">
                <el-select v-model="searchValue" @keyup.enter="doRender" size="large" filterable remote reserve-keyword
                  placeholder="请输入姓名或手机号搜索" :remote-method="handleChange" :loading="loading">
                  <el-option v-for="item in remoteOptions" :key="item.value" :label="item.label" :value="item.value" />
                </el-select>
                <i class="search-icon" @click="doRender">
                  <SearchOutlined />
                </i>
              </div>
              <div class="btns" v-if="route.query.withnext">
                <!-- <div class="next" style="color: #fff;" @click="doRender">搜索</div> -->
                <div :class="[resumeIndex === 0 ? 'disable' : '']" @click="prev">上一份</div>
                <div :class="[resumeIndex === resumeList?.length - 1 ? 'disable' : '']" @click="next">
                  下一份</div>
              </div>
            </div>
          </div>
          <div class="left-inner">
            <div class="switch" v-if="store.currentResume.processSort >= 11">
              <div v-for="item in switchList" v-show="item.show" :key="item.label"
                :class="[item.active == active ? 'active' : '']" class="items" @click="switChange(item)">{{
                  item.label }}
              </div>
            </div>
            <span class="lb-top">
              <StuInfo :currentResume="currentResume"></StuInfo>
            </span>
            <!-- 简历详情页面 -->
            <div class="lb-box">
              <NoAppointment></NoAppointment>
              <NoAgreeMent></NoAgreeMent>
              <NoBoarding></NoBoarding>
              <component :is="LBcomponentId" :currentResume="currentResume"></component>
            </div>
          </div>
        </div>
        <div class="right">
          <div class="right-top">
            <RTComponent :currentResume="currentResume"></RTComponent>
          </div>
          <div class="right-bottom">
            <RBTabs :currentResume="currentResume"></RBTabs>
          </div>
        </div>
      </div>
      <Record :currentResume="currentResume"></Record>

      <div class="tooltip  " ref="tooltipRef" :style="style" v-if="route.query.withnext">
        <img src="@/assets/images/applicationDetail/file.png" alt="" srcset="">
        <a-badge :count="resumeList.length" class="badge">
        </a-badge>
        <div class="ul-wrap">
          <ul class="animate__animated animate__fadeInUp animate__faster">
            <li v-for="item in resumeList" @click="setActiveResume(item)"
              :class="[nowResume.value === item.value ? 'tooltip-active' : '',]">
              <span class="ell">
                {{
                  item.label
                }}
              </span>
            </li>
          </ul>
        </div>
      </div>
    </template>
  </div>
</template>
<script setup lang='ts'>
import { ref, watch, onMounted, computed } from "vue";
import { SearchOutlined } from '@ant-design/icons-vue'
import { ElSelect, ElOption } from "element-plus";
import StuInfo from './LTComponent/StuInfo.vue';
import CvDetails from "./LBComponent/CvDetails.vue";
import Link from "./LBComponent/Link.vue";
import Question from "./LBComponent/Question.vue";
import RTComponent from "./RTComponent/Index.vue"
import RBTabs from "./RBComponent/RBTabs.vue";
import Record from "./Record/Record.vue";
import { comStores } from '@/store/common/comStore';
import { useRoute } from "vue-router";
import { searchResumesByName } from "@/api/application-details/index.ts";
import { idList } from "@/api/today-event/todo.ts";
import NoAppointment from "./LBComponent/NoAppointment.vue";
import NoAgreeMent from "./LBComponent/NoAgreeMent.vue";
import NoBoarding from "./LBComponent/NoBoarding.vue";
import { useRouter } from "vue-router";
import { message } from "ant-design-vue";
import { useDraggable } from '@vueuse/core'
import { getUserIDList } from '@/api/train/detail.ts'

const resumeIdLoadding = ref(false)
const store = comStores()
const LBcomponentId = ref(Link)
const route: any = useRoute();
const query = route.query;
const active = ref<number>(1)
const resumeId = ref<number>(+route.query.id)
const currentResume = ref({})
const switchList = ref([
  { label: "简历", component: CvDetails, active: 1, show: true },
  { label: "评价", component: Link, active: 2, show: false },
  { label: "问题", component: Question, active: 3, show: true },
])
const searchValue = ref<string>("");
const remoteOptions = ref<{ label: string; value: string }[]>([]);
const loading = ref<boolean>(false);
const router = useRouter()
const handleChange = async (val) => {
  const data = await searchResumesByName({ searchName: val.trim() });
  if (data && data.code == '10000') {
    remoteOptions.value = data.data.map(el => {
      return {
        label: el.searchName,
        value: el.resumesId
      }
    })
  } else {
    remoteOptions.value = []
  }
}


const getResumeInfo = async () => {
  await store.getResumeInfo(resumeId.value)
  currentResume.value = store.currentResume
  resumeIdLoadding.value = true
}
const resumeList = ref<any>([]);
const flterResumeList = computed(() => {
  return resumeList.value.slice(resumeIndex.value, 10)
})
const resumeIndex = ref<number>(-1);
const prev = () => {
  if (resumeIndex.value <= 0) {
    resumeIndex.value = 0
    return message.error("已经是第一条了")
  }
  resumeIndex.value--;
  let current = resumeList.value[resumeIndex.value];
  if (current) {
    router.push({ path: '/applicationDetails/cvDetail', query: { id: +current.value, withnext: "true",from:query.from } })
    sessionStorage.setItem("cacheResume", JSON.stringify(current))
  }
}
const next = () => {
  if (resumeIndex.value === resumeList.value.length - 1) {
    resumeIndex.value = resumeList.value.length - 1
    return message.error("已经是最后一条了")
  }
  resumeIndex.value++;
  let current = resumeList.value[resumeIndex.value];
  if (current) {
    router.push({ path: '/applicationDetails/cvDetail', query: { id: +current.value, withnext: "true",from:query.from } })
    sessionStorage.setItem("cacheResume", JSON.stringify(current))
  }
}
const nowResume = ref<any>({})
onMounted(async () => {
  resumeList.value = JSON.parse(sessionStorage.getItem("cacheResumeList") || "[]");
  nowResume.value = JSON.parse(sessionStorage.getItem("cacheResume") || "{}");
  resumeIndex.value = resumeList.value.findIndex(el => el.value == nowResume.value.value);
  let el = document.querySelector(".scroll-container") as HTMLDivElement;
  x.value = el.offsetWidth + 30
})
const setActiveResume = (val) => {
  router.push({ path: '/applicationDetails/cvDetail', query: { id: +val.value, withnext: "true",from:query.from } })
  sessionStorage.setItem("cacheResume", JSON.stringify(val))
}


watch(() => store.currentResume.processSort, (val) => {
  if (val < 11) {
    LBcomponentId.value = CvDetails
    active.value = 1
  } else if (val == 11) {
    LBcomponentId.value = Question;
    switchList.value[0].show = true
    switchList.value[1].show = false
    switchList.value[2].show = true
    active.value = 3
  } else {
    LBcomponentId.value = Link;
    switchList.value[0].show = true
    switchList.value[1].show = true
    switchList.value[2].show = false;
    active.value = 2
  }
}, { immediate: true })
watch(() => store.currentResume.processSort, () => {
  if (route.query.withnext) {
    getIdList()
  }
})
const getIdList = async () => {
  const cacheParam = JSON.parse(sessionStorage.getItem("cacheParam") || "{}")

  let data;
  if (query.from == 'trainDetail') {
    data = await getUserIDList(cacheParam)
  } else {
    data = await idList(cacheParam);
  }
  if (data && data.length > 0) {
    resumeList.value = data;
    sessionStorage.setItem("cacheResumeList", JSON.stringify(data));
  }
}

getResumeInfo()
const switChange = (item) => {
  active.value = item.active;
  LBcomponentId.value = item.component
}
const doRender = async () => {
  console.log(searchValue.value)
  if (!searchValue.value) return
  router.push({ path: '/applicationDetails/cvDetail', query: { id: +searchValue.value } })
}
const tooltipRef = ref<HTMLElement | null>(null);
const asideWith = ref<number>(40);
const { x, y, style } = useDraggable(tooltipRef, {
  initialValue: { x: asideWith.value, y: 700 },
})
</script>
<style lang="less" scoped>
.cv-wrap {
  width: 100%;
  height: 100%;
}

#cv-detail {
  box-sizing: border-box;
  width: 100%;
  overflow: hidden;
  display: flex;
  position: relative;

  .left {
    flex: 1;
    margin-right: 16px;
    overflow-x: hidden;

    .search-container {
      display: flex;
      align-items: center;
      margin-bottom: 20px;
      position: relative;
      justify-content: space-between;

      .title {
        font-weight: 600;
        font-size: 20px;
        color: #333333;
        line-height: 33px;
        text-align: left;
        word-break: keep-all;
      }

      .wrap {
        display: flex;
      }

      .search-input {
        width: 200px;
        height: 40px;
        font-size: 16px;
        border-radius: 7px;
        position: relative;
        cursor: pointer;
        user-select: none;

        :deep(.el-select__wrapper) {
          height: 100%;
          line-height: 45px;
          border: 1px solid rgba(179, 192, 200, 0.3);
          padding-left: 20px;
          box-sizing: border-box;
          padding-right: 30px;
        }

        .search-icon {
          position: absolute;
          font-size: 20px;
          right: 10px;
          top: 55%;
          transform: translateY(-50%);
          z-index: 9;
        }
      }

      .btns {
        display: flex;
        gap: 8px;
        user-select: none;
        margin-left: 20px;

        div {
          width: 100px;
          height: 40px;
          border-radius: 7px;
          text-align: center;
          line-height: 40px;
          font-size: 16px;
          color: #fff;
          cursor: pointer;
          background: #209EE4;
          user-select: none;
        }

        .disable {
          background: #8B9FAB;
        }
      }
    }

    .left-inner {
      position: relative;
      height: 81.69vh;
      overflow: auto;
      border-radius: 10px;
      background: #fff;
      padding: 30px;
      box-sizing: border-box;
      overflow-x: hidden;
      display: flex;
      flex-direction: column;
      position: relative;

      .lb-top {}

      .lb-box {
        // height: calc(100%);
        overflow: auto;
      }
    }
  }

  .right {
    flex: 1.12;
    height: 100%;
    display: flex;
    flex-direction: column;
    height: 88vh;

    .right-top {
      background: #fff;
      border-radius: 10px;
      min-height: 100px;
      margin-bottom: 16px;
      padding: 34px 40px;
      box-sizing: border-box;
    }

    .right-bottom {
      flex: 1;
      overflow: auto;
      background: #fff;
      border-radius: 10px;
      padding: 17px 40px;
      box-sizing: border-box;
      overflow: auto;
      padding-top: 6px;
    }
  }
}



.intention {
  .intention-title {
    font-weight: 500;
    font-size: 20px;
    color: #333333;
    line-height: 23px;
    text-align: left;
    margin-bottom: 16px;
  }
}

.switch {
  position: absolute;
  right: 30px;
  top: 30px;
  width: 100px;
  height: 32px;
  background: #209EE4;
  border-radius: 6px 6px 6px 6px;
  display: flex;
  align-items: center;
  padding: 0 2px;
  box-sizing: border-box;

  .items {
    width: 48px;
    height: 28px;
    text-align: center;
    line-height: 28px;
    border-radius: 6px 6px 6px 6px;
    cursor: pointer;
    color: #fff;
  }
}

.active {
  color: #000 !important;
  background: #fff;
}

.tooltip {
  position: fixed;
  cursor: pointer;
  width: 56px;
  height: 56px;
  background: #FFFFFF;
  box-shadow: 0px 0px 4px 0px rgba(31, 62, 108, 0.25);
  border-radius: 0px 0px 0px 0px;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 12px;
  z-index: 999;

  img {
    width: 24px;
    height: 30px;
  }

  .ul-wrap {
    position: absolute;
    width: 120px;
    height: 692px;
    position: absolute;
    background-color: transparent;
    bottom: -0px;
    display: none;
    padding-left: 20px;
    box-sizing: border-box;

    ul {
      position: absolute;
      bottom: 56px;
      width: 72px;
      max-height: 692px;
      background: #FFFFFF;
      box-shadow: 0px 0px 4px 0px rgba(31, 62, 108, 0.25);
      border-radius: 6px 6px 6px 6px;
      text-align: center;
      overflow: auto;
      flex-direction: column;
      padding: 10px;
      box-sizing: border-box;

      li {
        height: 37px;
        align-items: center;
        display: flex;
        border-bottom: 1px solid #ccc;
        width: 100%;
        justify-content: center;

        &:last-child {
          border: unset;
        }

      }

    }
  }

  .badge {
    position: absolute;
    right: -7px;
    top: 0px;
  }

  &:hover .ul-wrap {
    display: block;
  }
}

.tooltip-active {
  color: #209eff
}

.ell {
  overflow: hidden;
  text-overflow: ellipsis;
  word-break: break-word;
  overflow-wrap: break-word;
  white-space: nowrap;

}
</style>
